package com.zzb.framework.role.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zzb.framework.role.dao.SysRoleMenuDAO;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.role.entity.SysRoleMenu;

import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.BaseService;
import com.zzb.framework.common.ServiceException;
import com.zzb.framework.validator.annotation.ValidatorEnabled;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* 菜单角色关系表 service类
*/
@Service
public class SysRoleMenuService extends BaseService<SysRoleMenuDAO,SysRoleMenu> {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleMenuService.class);
    @Autowired
    private SysRoleMenuDAO sysRoleMenuDAO;
    @Override
    public SysRoleMenuDAO getDAO() {
        return sysRoleMenuDAO;
    }
    /**删除菜单角色关系表*/
    public BaseResponse deleteRoleMenu(String id,IUser operate) throws ServiceException{
        //TODO  ZZB 20170222
        return deleteById(id);
    }
    /**保存菜单角色关系表*/
    @ValidatorEnabled
    public BaseResponse<SysRoleMenu> saveRoleMenu(SysRoleMenu sysRoleMenu, IUser operator) throws ServiceException{
        logger.info("正在保存菜单角色关系表["+ JSON.toJSONString(sysRoleMenu)+"]["+JSON.toJSONString(operator)+"]");
        //TODO  ZZB 20170222
        return insertOrUpdate(sysRoleMenu,operator);
    }

    @Transactional
    public void removeByRoleIdAndMenuId(String roleId, String menuId){
        getDAO().getMapper().delete(new EntityWrapper<SysRoleMenu>().eq("role_id",roleId).eq("menu_id",menuId));
    }

    @Transactional
    public void removeByRoleId(String roleId){
        getDAO().getMapper().delete(new EntityWrapper<SysRoleMenu>().eq("role_id",roleId));
    }

    /**通过角色id和菜单id创建授权表*/
    public BaseResponse createByRoleIdAndMenuId(String roleId, String menuId, IUser operator) {
        try {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setMenuId(menuId);
            return insert(sysRoleMenu,operator);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }
}
